const bcrypt = require("bcrypt");
const moment = require("moment");

module.exports = {
  // 密码加密
  genSaltPassword(password) {
    const saltRounds = 10;
    return new Promise((resolve, reject) => {
      bcrypt.genSalt(saltRounds, (err, salt) => {
        bcrypt.hash(password, salt, (err, hash) => {
          err ? reject(err) : resolve(hash);
        });
      });
    });
  },

  //   判断密码
  comparePassword(password, hashPassword) {
    return new Promise((resolve, reject) => {
      bcrypt.compare(password, hashPassword, (err, isMatch) => {
        err ? reject(err) : resolve(isMatch);
      });
    });
  },

  success({ ctx, res = null }) {
    ctx.status = res.status ? res.status : 200;
    if (res.status) {
      delete res.status;
    }
    ctx.body = {
      ...res,
      data: res.data ? res.data : null,
      code: res.code ? res.code : 200,
      msg: res.msg ? res.msg : "请求成功",
    };
  },

  // isEmpty(params){
  //   return params === '' || params === null || params === undefined
  // }
  filterEmptyField(params) {
    let pam = {};
    for (let i in params) {
      if (params[i]) {
        if (i !== "page" && i !== "pageSize") {
          pam[i] = params[i];
        }
      }
    }
    return pam;
  },
  moment (){
    return moment
  },
};
